package Demo34;

import java.util.Arrays;

/**
 * 这里的都是位运算的练习
 */
class Solution {
    /**
     * 位1的个数
     * https://leetcode.cn/problems/number-of-1-bits/description/
     * @param n
     * @return
     */

    public int hammingWeight(int n) {
       int ret = 0;
       while(n > 0) {
        ret += n & 1;
        n >>= 1;
       }
       return ret;
    }

    /**
     * 比特位计数
     * https://leetcode.cn/problems/counting-bits/description/
     * @param n
     * @return
     */

    public int[] countBits(int n) {
        int[] ret = new int[n+1];
        for(int i = 1; i <= n; i++) {
            int num= i;
            int count = 0;
            while(num > 0) {
                count += num & 1;
                num >>= 1;
            }
            ret[i] = count;
        }
        return ret;
    }
    /**
     * 汉明距离
     * https://leetcode.cn/problems/hamming-distance/description/
     */
    public int hammingDistance(int x, int y) {
        int n = x ^ y;
        int ret = 0;
        while(n > 0) {
            ret += n & 1;
            n >>= 1;
        }
        return ret;
    }
    /**
     * 只出现一次的数字
     * https://leetcode.cn/problems/single-number/
     */
    public int singleNumber(int[] nums) {
        int len = nums.length;
        int ret = 0;
        for(int i = 0; i < len; i++) {
            ret ^= nums[i];
        }
        return ret;
    }
    /**
     *
     */

    public int[] singleNumber3(int[] nums) {
        int len = nums.length;
        int ret[] = new int[2];
        int num = 0;
        for(int i = 0; i < len; i++) {
            num ^= nums[i];
        }

        for(int i = 0; i < len;i++) {
            num ^= nums[i];
        }
        return ret;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ret = solution.singleNumber3(new int[] {1,2,1,3,2,5});
        for(int n : ret ) {
            System.out.print(n);
        }
    }
}